﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Drawing.Printing;
using WMS_BLL;
using WMS_DATABASE_MYSQL.MappingClass;
using WMS_DATABASE_MYSQL.MappingClass.CategoryClass;
using WMS_IBLL;
using WMS_WEBAPI.Controllers.Base;


namespace WMS_WEBAPI.Controllers.Table
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class CategoryController : ControllerBase
    {
        private readonly ILogger<CategoryController> _logger;
        private readonly string controllerName;
        private readonly ICateGoryIBLL _cateGoryIBLL;
        private readonly ICateGoryClassIBLL _cateGoryClassIBLL;
        public CategoryController(ILogger<CategoryController> logger,  ICateGoryIBLL cateGoryIBLL, ICateGoryClassIBLL cateGoryClassIBLL)
        {
            _logger = logger;
            controllerName = GetType().Name;
            _cateGoryIBLL = cateGoryIBLL;
            _cateGoryClassIBLL = cateGoryClassIBLL;
        }
        //以下为管理员及以上角色才能使用的方法
        //[Authorize(Roles = "superadmin,admin")]
        [HttpGet]
        public async Task<IActionResult> GetCategoryAsync(int current, int pageSize, int? type, int? category, string? description)
        {
            var data1 = await _cateGoryIBLL.QueryListAsync(a => (type == null || a.type == type)&& (category == null || a.category == category) && (description == null || a.description == description));
            var datacount = data1.Count();
            var data2 = await _cateGoryClassIBLL.QueryListAsync(a => true);
            //联表查询
            var combinedresults = data1.Join(data2,t1 => t1.category,t2 => t2.category,(t1, t2) => new wms_categoryjoincategoryclass
            {
              key = t1.key,
              category = t1.category,
              categorydesc = t2.description,
              type = t1.type,
              description = t1.description
            }).ToList();

            var data = combinedresults.Skip((current - 1) * pageSize).Take(pageSize);
            return Ok(new { data, total = datacount,suceess=true });
        }

        /// <summary>
        /// 获取所有数据库里的类别信息，用于前端类型信息表里的新建表里的选择框里的内容生成
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCategoryClassAsync()
        {
            var data = await _cateGoryClassIBLL.QueryListAsync(a=>true);
            return Ok(data);
        }


        [HttpDelete]
        public async Task<IActionResult> DelCategoryAsync([FromBody] List<int> key)
        {
            var t=await _cateGoryIBLL.DeleteRangeAsync(a=>key.Contains(a.key));
            if (t > 0) {
                return Ok("成功删除");
            }
            return BadRequest();
        }

        [HttpPut]
        public async Task<IActionResult> EditCategoryAsync([FromBody] wms_category_table data)
        {
            var t = await _cateGoryIBLL.EditAsync(data);
            if (t)
            {
                return Ok("成功修改");
            }
            return BadRequest();
        }
        [HttpPost]
        public async Task<IActionResult> AddCategoryAsync([FromBody] wms_category_table data)
        {
            try
            {
                var t = await _cateGoryIBLL.CreateAsync(data);
                if (t)
                {
                    return Ok("成功添加");
                }
                return BadRequest();
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Duplicate"))
                {
                    return BadRequest("Duplicate");
                }
                return BadRequest();
            }
           
        }
    }
}
